<template>
  <div class="user-list">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg">
        <label>用户类型</label>
        <el-select v-model="searchForm.real_type" placeholder="全部" size="small">
          <el-option label="全部" value></el-option>
          <el-option label="中国大陆" value="CN"></el-option>
          <el-option label="非中国大陆" value="non-CN"></el-option>
          <el-option label="未实名" value="unAuth"></el-option>
        </el-select>
      </div>
      <div class="item-msg" v-if="$store.state.userInfo.group === 'administrator'">
        <label>钱包</label>
        <el-select v-model="searchForm.platform_id" placeholder="全部" size="small">
          <el-option v-for="item in wallets" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div class="item-msg combain">
        <label>精确查找</label>
        <el-input
          placeholder="UID/姓名/手机号/推客ID"
          v-model="searchForm.search"
          class="input-with-select"
          size="small"
          clearable
        >
          <el-select v-model="searchForm.search_type" slot="prepend" placeholder="请选择">
            <el-option label="用户" value="user"></el-option>
            <el-option label="推客" value="inviter"></el-option>
          </el-select>
        </el-input>
      </div>
      <br>
      <div class="item-msg">
        <label>注册时间</label>
        <el-date-picker
          size="small"
          v-model="searchForm.times"
          type="daterange"
          value-format="timestamp"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <el-button class="search-btn" @click="initSearch(searchForm)" size="small" type="primary">查询</el-button>
      <el-button class="search-btn" size="small" @click="showDialog = true">导出</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="small"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="uid" label="UID" width="120"></el-table-column>
        <el-table-column align="center" label="姓名" width="120">
          <template slot-scope="item">{{item.row.name||'/'}}</template>
        </el-table-column>
        <el-table-column align="center" prop="mobile" label="手机"></el-table-column>
        <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
        <el-table-column align="center" prop="card_type" label="证件类型" width="170"></el-table-column>
        <el-table-column align="center" prop="card_id" label="证件号" width="180"></el-table-column>
        <el-table-column align="center" label="资产总值">
          <template slot-scope="item">
            <el-button type="text" @click="viewAssets(item.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" width="165" label="注册时间"></el-table-column>
        <el-table-column align="center" prop="inviter" label="推客"></el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>
      <!-- 查看资产弹窗 -->
      <transition name="fade">
        <assets-box
          v-if="showAssets"
          @cancel="close"
          @transaction="transaction"
          :assets="userAssets"
          :uid="viewUid"
        ></assets-box>
      </transition>
      <!-- 资产划转 -->
      <transition name="fade">
        <AssetsTransaction
          v-if="showAssetsTransaction"
          @cancel="AssetsTransactionClose"
          @success="AssetsTransactionSuccess"
          :type="TransactionType"
          :uid="viewUid"
        ></AssetsTransaction>
      </transition>
      <!-- 导出excel -->
      <transition name="fade">
        <excelDialog
          v-if="showDialog"
          @cancel="showDialog=false"
          @success="showDialog=false;"
          :params="searchForm"
        ></excelDialog>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import AssetsBox from "@/components/UserList/AssetsDialog.vue";
import AssetsTransaction from "@/components/UserList/AssetsTransaction.vue";
import excelDialog from "@/components/UserList/ExcelDialog";
import { getUserList, viewUserAssets } from "./User.service";

export default {
  components: {
    Pages,
    AssetsBox,
    AssetsTransaction,
    excelDialog
  },
  computed: {
    wallets() {
      return this.$store.state.wallet_list || [];
    }
  },
  data() {
    return {
      loading: false,
      searchForm: {
        platform_id: "",
        real_type: "",
        search_type: "user",
        search: "",
        start_time: "",
        end_time: "",
        times: null,
        page: 1,
        size: 10,
        excel: 0 //1--true,0-false
      },
      viewUid: "",
      userAssets: [],
      roleList: [],
      tableData: [],
      total: 10,
      showAssets: false,
      showAssetsTransaction: false,
      TransactionType: 1,
      showDialog: false
    };
  },
  watch: {
    "searchForm.times": function(time) {
      if (time) {
        this.searchForm.start_time = time[0] / 1000;
        this.searchForm.end_time = time[1] / 1000;
      } else {
        this.searchForm.start_time = this.searchForm.end_time = "";
      }
    }
  },
  mounted() {
    this.search(this.searchForm);
  },
  methods: {
    initSearch(form) {
      this.searchForm.page = 1;
      this.search(form);
    },
    async search(form) {
      this.loading = true;
      const res = await getUserList(form);
      console.log(res);
      if (res && res.code === 1) {
        this.total = Number(res.data.count) || 10;
        this.tableData = res.data.data;
      } else if (res.code === -1) this.searchForm.platform_id = "";
      this.loading = false;
    },
    async viewAssets(user) {
      this.showAssets = true;
      this.viewUid = user.uid;
      const res = await viewUserAssets(user.uid);
      if (res && res.code === 1) {
        this.userAssets = res.data;
      }
      // console.log(this.userAssets);
    },
    close() {
      this.showAssets = false;
      this.userAssets = [];
    },
    transaction(type) {
      this.TransactionType = type;
      this.showAssetsTransaction = true;
    },
    AssetsTransactionClose() {
      this.showAssetsTransaction = false;
    },
    AssetsTransactionSuccess() {
      this.AssetsTransactionClose();
      this.close();
      this.search(this.searchForm);
    },
    changeWallet(val) {
      if (typeof this.searchForm.platform_id !== String)
        this.searchForm.platform_id = val;
      this.initSearch(this.searchForm);
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss" scoped>
.user-list {
  // position: relative;
  min-height: calc(100vh - 100px);

  .search-block {
    width: 100%;
    padding: 25px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;

    .item-msg {
      display: inline-block;
      margin-right: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;

      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }

      .el-input {
        width: 150px;
      }

      .el-select {
        width: 150px;
      }

      .el-date-editor {
        width: 270px;
      }
    }

    .combain {
      .el-select {
        width: 80px;
      }

      .el-input {
        width: 280px;
      }
    }

    .search-btn {
      margin-left: 25px;
    }
  }

  .data-table {
    // position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }

  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */
 {
    opacity: 0;
  }
}
</style>
